#!/usr/bin/env python
#
# Copyright 2008 Allen Hutchison (allen@hutchison.org)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# $URL: http://severedelays.googlecode.com/svn/trunk/appengine/lib/request_handler.py $
__version__ = "$Rev: 33 $"
__author__ = "Allen Hutchison (allen@hutchison.org)"

"""A base request handler for all template-based web requests

This class handles the mechanics of serving a template-based page. It should be
the parent class of any request page (see current.py)
"""

import os

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

class BaseRequest(webapp.RequestHandler):
  def generate(self, template_name=None, template_values={}, expires=None, 
               content_type=None, data=None):
    if expires:
      self.response.headers.add_header("Expires", 
          expires.strftime("%a, %d %b %Y %H:%M:%S GMT"))
    
    if content_type:
      self.response.headers["Content-Type"] = content_type
    
    if template_name:
      values = {'application_name': 'Severe Delays',}
      values.update(template_values)
      directory = os.path.dirname(__file__)
      path = os.path.join(directory, os.pardir, 'templates', template_name)
      self.response.out.write(template.render(path, values))
    elif data:
      self.response.out.write(data)
